package com.summer.shopping.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.dto.PageDto;
import com.summer.shopping.domain.table.Bag;
import com.summer.shopping.domain.table.Quan;
import com.summer.shopping.domain.vo.QuanVO;
import com.summer.shopping.domain.vo.QuanVOComposite;
import com.summer.shopping.enums.bag.BagGoodsStatusEnums;
import com.summer.shopping.enums.bag.BagGoodsTypeConstant;
import com.summer.shopping.service.BagService;
import com.summer.shopping.mapper.BagMapper;
import com.summer.shopping.service.QuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author 86136
* @description 针对表【bag(背包表)】的数据库操作Service实现
* @createDate 2025-06-22 10:57:56
*/
@Service
public class BagServiceImpl extends ServiceImpl<BagMapper, Bag>
    implements BagService{

    @Autowired
    QuanService quanService;


    @Override
    public void addOneQuan(Long userId, String bagGoodsTypeConstant, Long goodsId,Integer expireDays) {
        Bag item = Bag.getBagOfOneItem(userId, goodsId, LocalDateTime.now().plusDays(expireDays), BagGoodsTypeConstant.QUAN);
        save(item);
    }

    @Override
    public List<Bag> getMyBag(Long userId, PageDto pageDto) {
        Page<Bag> page = pageDto.getMybatisPage(Bag.class);
        return lambdaQuery()
                .eq(Bag::getUserId, userId)
                .orderByDesc(Bag::getCreateTime)
                .page(page)
                .getRecords();
    }

    public List<Bag> getAllMyBag(Long userId){
        return lambdaQuery()
                .eq(Bag::getUserId, userId)
               .list();
    }

    @Override
    public QuanVOComposite getMyQuan(Long userId, PageDto pageDto) {
        ArrayList<QuanVO> list = new ArrayList<>();
        Page<Bag> page = lambdaQuery()
                .eq(Bag::getUserId, userId)
                .orderByDesc(Bag::getCreateTime)
                .page(pageDto.getMybatisPage(Bag.class));
        List<Bag> records = page.getRecords();
        if (records.size()==0){
            return new QuanVOComposite(list,0L);
        }
        Map<Long,Quan> quans = quanService.listByIds(records.stream().map(Bag::getGoodsId).toList()).stream()
                .collect(Collectors.toMap(Quan::getId, Function.identity()));

        for (int i = 0; i < records.size(); i++) {
            Bag bag = records.get(i);
            boolean isExpire = bag.getExpireTime().isBefore(LocalDateTime.now());
            Integer status = bag.getStatus();
            //判断是否需要更新状态
            if (status == BagGoodsStatusEnums.USEFUL.getStatus() && isExpire) {
                bag.setStatus(BagGoodsStatusEnums.EXPIRED.getStatus());
                updateById(bag);
            }
            Long goodsId = bag.getGoodsId();
            String[] worth = quans.get(goodsId).getWorth().split(":");
            QuanVO item = QuanVO.builder()
                    .bagId(bag.getId())
                    .quanId(quans.get(goodsId).getId())
                    .name(quans.get(goodsId).getName())
                    .description(quans.get(goodsId).getDescription())
                    .value(new BigDecimal(worth[0]))
                    .userLimitValue(new BigDecimal(worth[1]))
                    .expireTime(bag.getExpireTime())
                    .isExpire(isExpire)
                    .build();
            list.add(item);
        }

        return new QuanVOComposite(list,page.getTotal());
    }
}




